﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using DPW.Business.Model;
using DPW.Entity;
using ETL.Common.Logging;

namespace DPW.Business.Repository
{
    public class PoemRepository
    {
        public static PoemModel AddNewPoem(string poemName, string poemSummary, string poemContent, string poemAuthor, int categoryId)
        {
            try
            {
                var db = new DPWEntities();
                var newPoem = new Poem
                {
                    PoemId = GenerateNewPoemId(),
                    PoemName = poemName,
                    CategoryId = categoryId,
                    PoemContent = poemContent,
                    PoemAuthor = poemAuthor,
                    PoemSummary = poemSummary
                };
                db.Poems.Add(newPoem);
                db.SaveChanges();
                return new PoemModel(newPoem);

            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return null;
            }
        }

        public static int GenerateNewPoemId()
        {
            try
            {
                var db = new DPWEntities();
                var category = (from c in db.Poems
                                orderby c.PoemId descending
                                select c).FirstOrDefault();

                if (category != null)
                {
                    return category.PoemId + 1;
                }
                else return 1;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }
        }

        public static PoemModel GetPoemById(int poemId)
        {
            try
            {
                var db = new DPWEntities();
                var poem = (from p in db.Poems where p.PoemId == poemId select p).FirstOrDefault();

                if(poem != null)
                {
                    return new PoemModel(poem);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return null;
            }
        }

        public static List<PoemModel> GetOtherPoemsOfCategory(PoemModel currentPoem)
        {
            var poems = CategoryRepository.GetPoemsOfCategory(currentPoem.CategoryId);
            var lstP = new List<PoemModel>();
            if(poems != null && poems.Count() > 0)
            {
                var lstPoems = poems.ToList();
                foreach (var poemItem in lstPoems)
                {
                    if(poemItem.Id != currentPoem.Id)
                    {
                        lstP.Add(poemItem);
                    }
                }
            }
            return lstP;
        }

        public static List<PoemModel> GetOlderPoems(PoemModel currentPoem)
        {
            try
            {
                var db = new DPWEntities();
                var poems = from p in db.Poems
                            where
                                p.DateCreated.HasValue &&
                                p.DateCreated > currentPoem.DateCreated &&
                                p.PoemId != currentPoem.Id
                            select p;

                var lstP = new List<PoemModel>();
                if (poems != null && poems.Count() > 0)
                {
                    var lstPoems = poems.ToList();
                    foreach (var poemItem in lstPoems)
                    {
                        if (poemItem.PoemId != currentPoem.Id)
                        {
                            lstP.Add(new PoemModel(poemItem));
                        }
                    }
                }
                return lstP;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }

        }

        public static List<PoemModel> GetNewerPoems(PoemModel currentPoem)
        {
            try
            {
                var db = new DPWEntities();
                var poems = from p in db.Poems
                            where
                                p.DateCreated.HasValue &&
                                p.DateCreated <= currentPoem.DateCreated &&
                                p.PoemId != currentPoem.Id
                            select p;

                var lstP = new List<PoemModel>();
                if (poems != null && poems.Count() > 0)
                {
                    var lstPoems = poems.ToList();
                    foreach (var poemItem in lstPoems)
                    {
                        if (poemItem.PoemId != currentPoem.Id)
                        {
                            lstP.Add(new PoemModel(poemItem));
                        }
                    }
                }
                return lstP;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                throw;
            }
        }

        public static List<PoemModel> GetLatestPoems(int maxCount)
        {
            try
            {
                var db = new DPWEntities();
                var poems = (from p in db.Poems orderby p.DateCreated descending select p);
                var lstP = new List<PoemModel>();
                if (poems != null && poems.Count() > 0)
                {
                    var lstPoems = poems.ToList();
                    int i = 0;
                    foreach (var poemItem in lstPoems)
                    {
                        lstP.Add(new PoemModel(poemItem));
                        i++;
                        if(i == maxCount && maxCount != -1) break;
                    }
                }
                return lstP;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return null;
            }
        }

        public static List<PoemModel> GetMostViewedPoems(int maxCount)
        {
            try
            {
                var db = new DPWEntities();
                var poems = (from p in db.Poems orderby p.Views descending select p);
                var lstP = new List<PoemModel>();
                if (poems != null && poems.Count() > 0)
                {
                    var lstPoems = poems.ToList();
                    int i = 0;
                    foreach (var poemItem in lstPoems)
                    {
                        lstP.Add(new PoemModel(poemItem));
                        i++;
                        if (i == maxCount && maxCount != -1) break;
                    }
                }
                return lstP;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return null;
            }
        }

        public static PoemModel GetRandomPoem()
        {
            try
            {
                var poems = GetLatestPoems(-1);
                if(poems != null && poems.Count > 0)
                {
                    int k = poems.Count;
                    var rnd = new Random(DateTime.Now.Second + DateTime.Now.Minute);
                    int t = rnd.Next(k);

                    if (t <= k)
                        return poems[t];
                    else
                        return poems[0];
                }
                return new PoemModel();
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return new PoemModel();
                throw;
            }
        }

        public static void UpdatePoem(PoemModel poemModel)
        {
            try
            {
                var db = new DPWEntities();
                var poemEntity = (from p in db.Poems where p.PoemId == poemModel.Id select p).FirstOrDefault();
                if(poemEntity != null)
                {
                    poemEntity.Views = poemModel.Views;
                    db.Entry(poemEntity).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                
                throw;
            }
        }
     }
}
